package demo.hms.dashboard.service.serviceImpl;

import demo.hms.dashboard.dao.DashboardDao;
import demo.hms.dashboard.service.DashboardService;
import demo.hms.entity.Dashboard;
import demo.hms.util.Result;
import demo.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class DashboardServiceImpl implements DashboardService {
    @Resource
    private DashboardDao dao;

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

    @Override
    public Result selectRoomNumber() {
        int roomNumber = dao.selectRoomNumber();
        return ResultSubmit.success(roomNumber);
    }

    @Override
    public Result selectLiveNumber() {
        return ResultSubmit.success(dao.selectLiveNumber());
    }

    @Override
    public Result selectRevenue(String month) {
        return ResultSubmit.success(dao.selectRevenue(month +"-01 00:00:00"));
    }

    @Override
    public Result selectRevenueByMonth(String month) {
        List<Dashboard> 多月收入合计 = new ArrayList<>();
        for(int i = 0; i<6 ; i++){
            Dashboard dashboard = new Dashboard();
            dashboard.setMonth(month);
            dashboard.setRevenue(dao.selectRevenue(month+"-01 00:00:00"));
            多月收入合计.add(dashboard);
            Date date = null;
            try{
                date = format.parse(month+"-01 00:00:00");
            }catch (ParseException e){
                e.printStackTrace();
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, -1);
            month = format.format(calendar.getTime());
        }
        return ResultSubmit.success(多月收入合计);
    }

}
